package com.yyg.boot.validator;

import com.yyg.boot.domain.User;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 * @Description Description
 * @Author 一一哥Sun
 * @Date Created in 2020/3/23
 */
@Component
public class EmailValidator implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        return User.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        User user = (User)target;
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "","Email is empty");
        if (!user.getEmail().contains("@")) {
            errors.rejectValue("email","", "Email is not valid.");
        }
    }

}
